package com.test.mchelepesa.bean_rice.config_info_rice;

import android.text.TextUtils;

import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.test.mchelepesa.util_rice.helper_rice.UserInfoHelper_rice;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;

public class AppShowInfo_rice {

    private static class Inner {
        private static AppShowInfo_rice instance = new AppShowInfo_rice();
    }

    private AppShowInfo_rice() {}

    public static AppShowInfo_rice getInstance() {
        return Inner.instance;
    }

    private Customize customize;
    private ArrayList<WalletChannel> walletChannels;

    public static void parse(JSONObject objJson) {
        //if (objJson==null) return;
        Gson gson = new Gson();
        Inner.instance = gson.fromJson(objJson.toString(), AppShowInfo_rice.class);
        if (Inner.instance.customize==null) Inner.instance.customize = gson.fromJson(objJson.optString("customize"),Customize.class);
    }

    /**
     * 配置的AF key
     */
    public String getAFKey() {
        if (customize==null) return "";
        return customize.getAfKey();
    }

    public String[] getMobile() {
        if (customize == null) return new String[0];
        return customize.getMobile();
    }

    public String[] getEmail() {
        if (customize == null) return new String[0];
        return customize.getEmail();
    }

    public String getTransferDelayImportant() {
        if (customize==null) return "";
        return StringUtil_rice.getSafeString(customize.getTransferDelayImportant());
    }

    public String getRejectPeriod() {
        if (customize==null) return "";
        return customize.getRejectPeriod();
    }

    public boolean showSysReview(){
        if (customize==null) return false;
        return customize.isShowSysReview();
    }

    public boolean canDeleteData(){
        if (customize == null) return false;
        return customize.canDeleteData();
    }

    public String getBindAccountTips(){
        if (customize == null) return "";
        return customize.getBindAccountTips();
    }

    public boolean isTestAccount(){
        return !TextUtils.isEmpty(isTestAccount(UserInfoHelper_rice.getInstance().getMobil_rice()));
    }

    public String isTestAccount(String mobile){
        if (customize == null || customize.getAccList() == null || TextUtils.isEmpty(mobile)) return "";
        ArrayList<LinkedHashMap<String,String>> accList = customize.getAccList();
        try {
            for (LinkedHashMap<String,String> acc:accList){
                if (
                        acc.containsKey("accM") &&
                        mobile.equalsIgnoreCase(acc.get("accM")) &&
                        acc.containsKey("accTk")
                ) return acc.get("accTk");
            }
        }catch (Exception e){
            StringUtil_rice.printException(e);
        }
        return "";
    }

    public String getCustomerHint(){
        if (customize==null) return "";
        return customize.customerServiceHint();
    }

    public String getReviewNotice(){
        if (customize==null) return "";
        return customize.getReviewNotice();
    }

    public String getLoaningNotice(){
        if (customize==null) return "";
        return customize.getLoaningNotice();
    }

    public String getRepayNotice(){
        if (customize==null) return "";
        return customize.getRepayNotice();
    }

    public static class Customize{

        private String homeAmount;
        private String showSysReview;
        private String noPointRecord;
        private String companyAddress;
        private String mobile;
        private String company;
        private String afKey;
        private String email;
        private String repayType;
        private String rejectPeriod;
        private JsonArray walletChannels;
        private String transferDelayImportant;
        private String canDeleteData;
        private String bindAccountTips;
        private ArrayList<LinkedHashMap<String,String>> accList;

        private String customerServiceHint;

        private String reviewNotice;
        private String loaningNotice;
        private String repayNotice;

        private Customize(){}

        public String getHomeAmount() {
            return StringUtil_rice.getSafeString(homeAmount);
        }

        public String getNoPointRecord() {
            return StringUtil_rice.getSafeString(noPointRecord);
        }

        public String getCompanyAddress() {
            return StringUtil_rice.getSafeString(companyAddress);
        }

        public String[] getMobile() {
            return StringUtil_rice.getSafeString(mobile).split(",");
        }

        public String getCompany() {
            return StringUtil_rice.getSafeString(company);
        }

        public String getAfKey() {
            return StringUtil_rice.getSafeString(afKey);
        }

        public String[] getEmail() {
            return StringUtil_rice.getSafeString(email).split(",");
        }

        public String getRepayType() {
            return StringUtil_rice.getSafeString(repayType);
        }

        public String getRejectPeriod() {
            return StringUtil_rice.getSafeString(rejectPeriod);
        }

        public JsonArray getWalletChannels() {
            return walletChannels;
        }

        public String getTransferDelayImportant() {
            return StringUtil_rice.getSafeString(transferDelayImportant);
        }

        public String customerServiceHint() {
            if (TextUtils.isEmpty(customerServiceHint)) return "";
            return customerServiceHint;
        }

        public boolean isShowSysReview(){
            return StringUtil_rice.getSafeString(showSysReview).equalsIgnoreCase("YES");
        }

        public boolean canDeleteData(){
            if (TextUtils.isEmpty(canDeleteData)) return false;
            return canDeleteData.equalsIgnoreCase("YES");
        }

        public String getBindAccountTips() {
            return StringUtil_rice.getSafeString(bindAccountTips);
        }

        public ArrayList<LinkedHashMap<String,String>> getAccList() {
            return accList;
        }

        public String getReviewNotice(){
            return StringUtil_rice.getSafeString(reviewNotice);
        }

        public String getLoaningNotice(){
            return StringUtil_rice.getSafeString(loaningNotice);
        }

        public String getRepayNotice(){
            return StringUtil_rice.getSafeString(repayNotice);
        }

    }

    private void initWalletChannels(){

        walletChannels = new ArrayList<>();

        if (customize==null) return;

        JsonArray jsonArray = customize.getWalletChannels();
        Gson gson = new Gson();
        if (jsonArray!=null){
            for (int i=0;i<jsonArray.size();i++){
                WalletChannel walletChannel = gson.fromJson(jsonArray.get(i).toString(),WalletChannel.class);
                walletChannels.add(walletChannel);
            }
        }

    }

    public ArrayList<WalletChannel> getWalletChannels(){
        if (walletChannels==null){
            initWalletChannels();
        }

        return walletChannels;
    }

    public WalletChannel findChannelByValue(String value){
        if (TextUtils.isEmpty(value)) return null;
        ArrayList<WalletChannel> channels = getWalletChannels();
        if (channels == null || channels.isEmpty()) return null;
        for (WalletChannel channel:channels){
            if (value.equalsIgnoreCase(channel.getValue())) return channel;
        }
        return null;
    }

    public static class WalletChannel{

        private String desc;
        private String value;
        private String logo;

        public String getDesc() {
            return StringUtil_rice.getSafeString(desc);
        }

        public String getValue() {
            return StringUtil_rice.getSafeString(value);
        }

        public String getLogo() {
            return StringUtil_rice.getSafeString(logo);
        }

    }

}
